class Solution {
    public int countNumbersWithUniqueDigits(int n) {
      char[] s = Integer.toString(n).toCharArray();
      return dfs(0,0,true,false,s);
    }
    private int dfs(int i,int mask,boolean islimit,boolean isnum,char[] s)
    {
        if(i==s.length)
        {
            return isnum?1:0;
        }
        int res = 0;
        if(!isnum)
        {
            res = dfs(i+1,mask,false,false,s);
        }
        int up = islimit?s[i]-'0':9;
        for(int d = isnum?0:1;d<=up;d++)
        {
           if((mask>>d&1)==0)
           {
            res+=dfs(i+1,mask|(1<<d),islimit&&d==up,true,s);
           }
        }
        return res;
    }
}